package com.shanjupay.merchant.service.impl;

import com.shanjupay.common.domain.BusinessException;
import com.shanjupay.common.domain.CommonErrorCode;
import com.shanjupay.merchant.service.SmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Service;

import org.springframework.web.client.RestTemplate;


import java.util.HashMap;
import java.util.Map;

/**
 * @program: shanjupay
 * @description: **注意这个service注解不是dubbo的**
 * @author: Mr.Hkc
 * @create: 2020-08-18 15:53
 **/
@Service
public class SmsServiceImpl implements SmsService {


    @Value("${sms.url}")
    String url;

    @Value(("${sms.effectiveTime}"))
    String effectiveTime;

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 发送验证码
     *
     * @param phone
     * @return 验证码对应的key
     */
    @Override
    public String sendMsg(String phone) throws BusinessException{
        //向验证码发送请求获取验证码
        String sms_url = url + "/generate?name=sms&effectiveTime=" + effectiveTime;
        //请求体
        Map<String, Object> body = new HashMap<>();
        body.put("mobile", phone);
        //请求头
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        //请求信息,传入body,header
        HttpEntity httpEntity = new HttpEntity(body, httpHeaders);
        //向url请求
        ResponseEntity<Map> exchange = null;
        Map bodyMap = null;
        try {
            exchange = restTemplate.exchange(sms_url, HttpMethod.POST, httpEntity, Map.class);
            bodyMap = exchange.getBody();
        } catch (Exception e) {
            e.printStackTrace();
            //throw new RuntimeException("验证码发送失败");
            throw new BusinessException(CommonErrorCode.E_100102);
        }

        if (bodyMap == null || bodyMap.get("result") == null) {
            //throw new RuntimeException("发送验证码失败");
            throw new BusinessException(CommonErrorCode.E_100102);
        }
        Map result = (Map) bodyMap.get("result");
        String key = (String) result.get("key");
        return key;
    }

    /**
     * 校验验证码
     *
     * @param verifyKey  验证码的key
     * @param verifyCode 验证码
     */
    @Override
    public void checkVerifyCode(String verifyKey, String verifyCode) throws BusinessException {
        String url = "http://localhost:56085/sailing/verify?name=sms&verificationCode=" + verifyCode + "&verificationKey=" + verifyKey;
        Map bodyMap = null;
        try {
            //准备使用RestTemplate请求校验验证码服务
            ResponseEntity<Map> exchange = restTemplate.exchange(url, HttpMethod.POST, HttpEntity.EMPTY, Map.class);
            bodyMap = exchange.getBody();
        } catch (Exception e) {
            e.printStackTrace();
            //throw new RuntimeException("验证码校验失败");
            throw new BusinessException(CommonErrorCode.E_100102);
        }
        if (bodyMap == null || bodyMap.get("result") == null || !(Boolean) bodyMap.get("result")) {
            //throw new RuntimeException("校验验证码失败");
            throw new BusinessException(CommonErrorCode.E_100102);

        }

    }
}

